package com.leyou.seckill.controller;

import com.leyou.order.dto.OrderDTO;
import com.leyou.order.dto.SeckillOrderDTO;
import com.leyou.seckill.pojo.Seckill;
import com.leyou.seckill.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 秒杀Controller
 *
 * @Author jie
 * @Date 2020/11/17
 */
@RestController
public class SeckillController {


    @Autowired
    private SeckillService seckillService;


    /**
     * 查询所有参与秒杀的商品
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    @GetMapping("/list")
    public ResponseEntity<List<Seckill>> seckills(){

        List<Seckill> seckills= seckillService.seckills();

        return ResponseEntity.ok(seckills);
    }


    /**
     * 创建秒杀订单
     * @param seckillOrderDTO
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    @PostMapping("/seckillOrder")
    public ResponseEntity<Long> buildSeckillOrder(@RequestBody SeckillOrderDTO seckillOrderDTO){

        Long seckillOrderId = seckillService.buildSeckillOrder(seckillOrderDTO);
        return ResponseEntity.ok(seckillOrderId);
    }



    /**
     * 根据id查询秒杀商品
     * @return
     */
    @GetMapping("/seckill")
    public ResponseEntity<Seckill> findSeckillById(Long id){
        Seckill seckill  = seckillService.findSeckillById(id);
        return ResponseEntity.ok(seckill);
    }


    /**
     * 根据id修改秒杀商品库存
     * @param cartMap
     * @return
     */
    @PutMapping("/updateSeckill")
    public ResponseEntity<Void> updateSeckill(Map<String,String> cartMap){

        seckillService.updateSeckill(cartMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
